package com.weiaibenpao.demo.chislim.ui.activity.me;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.base.BaseActivity;
import com.weiaibenpao.demo.chislim.bean.UserBean;
import com.weiaibenpao.demo.chislim.bean.UserDataBean;
import com.weiaibenpao.demo.chislim.db.preferences.UserPreferences;
import com.weiaibenpao.demo.chislim.presenter.LoginPresenter;
import com.weiaibenpao.demo.chislim.ui.activity.MainActivity;
import com.weiaibenpao.demo.chislim.ui.widget.CustomDatePicker;
import com.weiaibenpao.demo.chislim.ui.widget.ScaleView;
import com.weiaibenpao.demo.chislim.view.LoginView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class CompleteInfoActivity extends BaseActivity<LoginView, LoginPresenter> implements LoginView{
    @BindView(R.id.text_index)
    TextView textIndex;
    List<View> list;
    @BindView(R.id.com_radio_btn1)
    RadioButton comRadioBtn1;
    @BindView(R.id.com_radio_btn2)
    RadioButton comRadioBtn2;
    @BindView(R.id.com_group)
    RadioGroup comGroup;
    @BindView(R.id.tv_com_height)
    TextView tvComHeight;

    @BindView(R.id.update_datePicker)
    CustomDatePicker updateDatePicker;
    @BindView(R.id.btn_next)
    Button btnNext;
    @BindView(R.id.index_1)
    View view1;
    @BindView(R.id.index_2)
    View view2;
    @BindView(R.id.index_3)
    View view3;
    @BindView(R.id.tv_style)
    TextView tvStyle;
    @BindView(R.id.date_layout)
    View dateLayout;

    @BindView(R.id.tv_com_weight)
    TextView tvComWeight;
    @BindView(R.id.btn_back)
    Button btnBack;
    @BindView(R.id.ruler1)
    ScaleView ruler1;
    @BindView(R.id.ruler2)
    ScaleView ruler2;

    private int index = 0;

    private String sex = "男";
    private String birth = "1990-01-01";
    private float weight = 55.0f;
    private int height = 175;
    private int userId;
    private UserPreferences userPreferences;


    @Override
    public int setLayoutResourceID() {
        return R.layout.activity_complete_info;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        userPreferences = UserPreferences.getInstance();
        userId = userPreferences.getInt(UserPreferences.USER_ID);
        initView();
    }

    public static void startSelf(Context context){
        context.startActivity(new Intent(context,CompleteInfoActivity.class));
    }

    @Override
    public LoginPresenter initPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected void initImmersionBar() {
        immersionBar.fullScreen(true)
                .init();
    }
    private void initView() {
        list = new ArrayList<>();
        list.add(view1);
        list.add(view2);
        list.add(view3);
        list.add(dateLayout);
        comRadioBtn2.setChecked(true);
        /**
         * 设置选中的条目
         */

        ruler1.setOnSelectItem(new ScaleView.SelectItem() {
            @Override
            public int setSelectItem() {
                return 50;
            }
        });
        ruler2.setOnSelectItem(new ScaleView.SelectItem() {
            @Override
            public int setSelectItem() {
                return 171;
            }
        });
        /**
         * 监听拖动时值得变化
         */
        ruler1.setOnRulerValueChangeListener(new ScaleView.RulerValue() {
            @Override
            public void value(int i) {
                tvComWeight.setText(i + "kg");
                weight=i;
            }
        });
        ruler2.setOnRulerValueChangeListener(new ScaleView.RulerValue() {
            @Override
            public void value(int i) {
                tvComHeight.setText(i + "cm");
                height=i;
            }
        });

        comGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.com_radio_btn1:
                        sex = "女";
                        break;
                    case R.id.com_radio_btn2:
                        sex = "男";
                        break;
                }
            }
        });


        updateDatePicker.init(1990, 01, 01, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                birth = year + "-" + (monthOfYear + 1) + "-" + dayOfMonth;
                Logger.e("birth="+birth);
            }
        });
    }

    @OnClick({R.id.btn_next, R.id.btn_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_next:

                if (index<list.size()-1){
                    index++;
                    showIndex();
                }else {
                    login();
                }
//                if (index == 4) {
//
//                    index=4;
//                }


                break;
            case R.id.btn_back:
                index--;
                showIndex();
                break;
        }
    }

    private void login() {
        mPresenter.upDataInfo(userId,"","",sex,height,weight,birth,"");
    }

    private void showIndex() {
        if (index == 3) {
            btnNext.setText("完成");
        }else {
            btnNext.setText("下一步");
        }
            textIndex.setText(index+1  + "/4");
            showView();
    }
    private void showView() {
        if (index == 0) {
            btnBack.setVisibility(View.GONE);
        } else {
            btnBack.setVisibility(View.VISIBLE);
        }
        if (index == 1) {
            tvStyle.setText("体重");
        } else if (index == 2) {
            tvStyle.setText("身高");
        } else if (index == 3) {
            tvStyle.setText("生日");
        }else if (index==0){
            tvStyle.setText("性别");
        }
        for (int i = 0; i < list.size(); i++) {
            View view = list.get(i);
            if (index == i) {
                view.setVisibility(View.VISIBLE);
            } else {
                view.setVisibility(View.GONE);
            }
        }
        }

    @Override
    public void upDataInfoSuccess(UserBean userBean) {
        userPreferences.putUserData(userBean,true);
        MainActivity.startSelf(this);
        finish();
    }

    @Override
    public void loginSuccess(UserDataBean userDataBean) {

    }


    @Override
    public void seedSuccess(String str) {

    }



}
